<cfoutput>
	<cfset worksheets = application.s.worksheetService.getWorksheet() />
	<cfset worksheetID = $.request.getValue('worksheetID') />
	<cfif worksheetID eq ''><cfset worksheetID = "blank" /></cfif>
	<cfset worksheet = application.s.worksheetService.getWorksheet(worksheetID) />
	<cfset worksheetFields = application.s.worksheetService.getWorksheetFields(worksheetID) />
	<cfset types = queryNew("id,name") />
	<cfset queryAddRow(types) /><cfset querySetCell(types,"id",0) /><cfset querySetCell(types,"name","textarea") />
	<cfset queryAddRow(types) /><cfset querySetCell(types,"id",1) /><cfset querySetCell(types,"name","list") />
	
	<cfsavecontent variable="page">
		<script>
			loadWorksheet = function(worksheetID){
				Main.nav('worksheet_designer',false,false,'?worksheetID='+worksheetID);
			};
			deleteWorksheet = function(worksheetID){
				Main.func(
					'worksheetController',
					'deleteWorksheet',
					'&worksheetID='+worksheetID,
					'are you sure you want to delete this worksheet?'
				);
			};
			deleteWorksheetField = function(worksheetFieldID){
				Main.func(
					'worksheetController',
					'deleteWorksheetField',
					'&worksheetFieldID='+worksheetFieldID,
					'are you sure you want to delete this worksheet field?'
				);
			};
		</script>
		<div class="header variable-width">
			<i class="fa fa-home" onclick="Main.nav('home');"></i>
			<div class="header-title">Worksheet Designer</div>
			#$.render.img(path="craftivity/widget_icons/book.png",style="margin-bottom:0px;")#</br>
			#$.render.select(name="worksheetID",query=worksheets,value=worksheetID,option="- New Worksheet -",blank=true,onchange="loadWorksheet(this.value);",style="margin-bottom:4px;")#
		</div>
		
		#$.render.form(name='save_worksheet',class='widget widget-form',method='worksheetController.saveTheWorksheet')#
			<div class="widget-title"><i class="fa fa-file-text"></i>Worksheet</div>
			#application.v.craftivityView.renderInput(name="name",value=worksheet.name,varchar=50)#
			#application.v.craftivityView.renderInput(name="code",value=worksheet.code,varchar=50)#
			#application.v.craftivityView.renderInput(name="sort_order",value=worksheet.sort_order)#
			#application.v.craftivityView.renderTextarea(name="description",value=worksheet.description)#
			<cfif worksheetID eq 'blank'><cfset worksheetID = ''></cfif>
			<input type="hidden" name="worksheetID" value="#worksheetID#">
			
			<div class="buttons">
				<button type="button" name="edit" class="left" onclick="Widget.edit(this);">edit</button>
				<button type="submit" name="save" class="red right">save</button>
				<button type="button" name="cancel" class="grey right" onclick="Widget.cancel(this);">cancel</button>
				<cfif worksheetID neq "">
					<button type="button" name="delete" class="left grey" function="worksheetController.deleteWorksheet.&worksheetID=#worksheetID#" onclick="Widget.delete(this);">delete</button>
				</cfif>
			</div>
		</form>
		
		<cfloop query="worksheetFields">
			#$.render.form(name='save_worksheet_field_#worksheetFields.currentRow#',class='widget widget-form',method='worksheetController.saveTheWorksheetField')#
				<div class="widget-title"><i class="fa fa-keyboard-o"></i>Worksheet Field</div>
				#application.v.craftivityView.renderTextarea(name="name",value=worksheetFields.name)#
				#application.v.craftivityView.renderInput(name="field_number",value=worksheetFields.field_Number)#
				#application.v.craftivityView.renderTextarea(name="description",value=worksheetFields.description)#
				#application.v.craftivityView.renderSelect(name="field_type",blank=false,query=types,value=worksheetFields.field_type)#
				#application.v.craftivityView.renderInput(name="default_value",value=worksheetFields.default_value,varchar=2000)#
				<input type="hidden" name="worksheetID" value="#worksheetID#">
				<input type="hidden" name="worksheetFieldID" value="#worksheetFields.id#" />
				
				<div class="buttons">
					<button type="button" name="edit" class="left" onclick="Widget.edit(this);">edit</button>
					<button type="submit" name="save" class="red right">save</button>
					<button type="button" name="cancel" class="grey right" onclick="Widget.cancel(this);">cancel</button>
					<button type="button" name="delete" class="left grey"function="worksheetController.deleteWorksheetField.&worksheetID=#worksheetID#&worksheetFieldID=#worksheetFields.id#" onclick="Widget.delete(this);">delete</button>
				</div>
			</form>
		</cfloop>
		
		<cfif $.valid.uuid(worksheetID)>
			#$.render.form(name='save_worksheet_field_new',class='widget widget-form',method='worksheetController.saveTheWorksheetField')#
				<div class="widget-title"><i class="fa fa-keyboard-o"></i><span class="new">New</span> Worksheet Field</div>
				#application.v.craftivityView.renderTextarea(name="name",value="")#
				#application.v.craftivityView.renderInput(name="field_number",value="")#
				#application.v.craftivityView.renderTextarea(name="description",value="")#
				#application.v.craftivityView.renderSelect(name="field_type",blank=false,query=types,value="")#
				#application.v.craftivityView.renderInput(name="default_value",value="",varchar=2000)#
				<input type="hidden" name="worksheetID" value="#worksheetID#">
				<input type="hidden" name="worksheetFieldID" value="" />
				
				<div class="buttons">
					<button type="button" name="edit" class="left" onclick="Widget.edit(this);">edit</button>
					<button type="submit" name="save" class="red right">save</button>
					<button type="button" name="cancel" class="grey right" onclick="Widget.cancel(this);">cancel</button>
				</div>
			</form>
		</cfif>

	</cfsavecontent>
	
	#application.v.craftivityView.renderPage(page)#
	
</cfoutput>